package com.auth.leaves.common.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * @Author: LEAVES
 * @Date: 2020年10月29日 15时47分42秒
 * @Version 1.0
 * @Description:    时间工具类
 */
public class DateUtils {
    public static final String DATE_FORMART_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    /**
     * 指定日期加上天数后的日期
     * @param num 为增加的天数
     * @param currentDate 创建时间
     * @return
     */
    public static Date plusDay(int num, Date currentDate) {
        LocalDateTime localDateTime = dateConvertLocalDateTime(currentDate);
        localDateTime = localDateTime.plusDays(num);
        return localDateTimeConvertDate(localDateTime);
    }

    /**
     * 根据时间字符串转换成
     * @param currentDate
     * @param formateStr
     * @return
     */
    public static Date dateStrConvertDate(String currentDate, String formateStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formateStr);
        LocalDateTime localDateTime = LocalDateTime.parse(currentDate, dateTimeFormatter);
        return localDateTimeConvertDate(localDateTime);
    }

    /**
     * 根据date转换成localDateTime
     * @param date
     * @return
     */
    private static LocalDateTime dateConvertLocalDateTime(Date date) {
        LocalDateTime localDateTime = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
        return localDateTime;
    }

    /**
     * 根据localDateTime转换成date
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeConvertDate(LocalDateTime localDateTime) {
        return Date.from(getZonedDateTimeByLocalDateTime(localDateTime).toInstant());
    }

    /**
     * 根据时间转换成时间字符串
     * @param currentDate
     * @param formateStr
     * @return
     */
    public static String formatDateToParse(Date currentDate, String formateStr) {
        LocalDateTime localDateTime = dateConvertLocalDateTime(currentDate);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formateStr);
        return dateTimeFormatter.format(localDateTime);
    }
    /**
     * @description 根据开始时间，结束时间进行对比
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    public static boolean compareDay(Date startDate, Date endDate) {
        return startDate.before(endDate);
    }

    /**
     * 获取本月第一天
     * @return
     */
    public static Date getMonthFirstDay() {
        LocalDateTime date = LocalDateTime.now();
        LocalDateTime firstday = date.with(TemporalAdjusters.firstDayOfMonth());
        return localDateTimeConvertDate(firstday);
    }

    /**
     * 获取本月最后一天
     * @return
     */
    public static Date getMonthLastDay() {
        LocalDateTime date = LocalDateTime.now();
        LocalDateTime lastday = date.with(TemporalAdjusters.lastDayOfMonth());
        return localDateTimeConvertDate(lastday);
    }

    /**
     * 根据localDateTime转换成ZonedDateTime对象，用于把localDatTime转成Date
     * @param localDateTime
     * @return
     */
    public static ZonedDateTime getZonedDateTimeByLocalDateTime(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        return zonedDateTime;
    }

    /**
     * 获得某天最大时间  例如：2020-10-10 23:59:59
     * @param date
     * @return
     */
    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        ;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return localDateTimeConvertDate(endOfDay);
    }

    /**
     * 获得某天最小时间  例如：2020-10-10 00:00:00
     * @param date
     * @return
     */
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return localDateTimeConvertDate(startOfDay);
    }
}
